package cn.wolfcode.web.controller;


import cn.wolfcode.domain.City;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Province;
import cn.wolfcode.quest.JsonResult;
//import com.fasterxml.jackson.databind.ObjectMapper;
import cn.wolfcode.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@RequestMapping("/json")
@Controller
public class JsonController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/city")
    @ResponseBody
    public List<City> getCity(Long pid){
        //拿到省份的id了
        List<City> cities = City.getCityByProvinceId(pid);
        //城市对象集合
        return cities;
    }




    @RequestMapping("/provinces")
    @ResponseBody
    public List<Province> getPovinces(){
        //返回所有的省份信息
        List<Province> allProvince = Province.getAllProvince();
        return allProvince;
    }







    @RequestMapping("/loginJson")
    @ResponseBody
    public JsonResult checkUsername(String username,String password){
        JsonResult jsonResult = new JsonResult();
        System.out.println(username+"   "+password);
        if (!"root".equals(username)){
            jsonResult.setMsg("用户名错误");
            jsonResult.setSuccess(false);
            return jsonResult;
        }else if (!"admin".equals(password)){
            jsonResult.setMsg("密码错误");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
        jsonResult.setSuccess(true);
        jsonResult.setMsg("登录成功");

        return jsonResult;
    }


    @RequestMapping("/checkUsername")
    @ResponseBody
    public JsonResult checkUsername(String username){
        //判断用户名的是否正确
        JsonResult jsonResult = new JsonResult();
        if ("aaa".equals(username)){
            jsonResult.setSuccess(false);
            jsonResult.setMsg("用户名已注册");
        }else {
            jsonResult.setMsg("可以注册");
            jsonResult.setSuccess(true);
        }
        return jsonResult;

    }








    @RequestMapping("/resp")
    public void respToBrow(HttpServletResponse response) throws Exception {
        /*response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("{\"id\":1,\"name\":\"张三\",\"sn\":\"zs\"}");//{"id":1,"name":"张三","sn":"zs"}
        //利用相关API将对象转换为Json字符串
        Department department = new Department(1l,"李四",new Date().toLocaleString());
        writer.write(JSON.toJSONString(department));
        writer.flush();*/
    }

    @RequestMapping("/gettime")
    @ResponseBody
    public JsonResult getTime(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        jsonResult.setMsg(new Date().toLocaleString());
        return jsonResult;
    }

    @RequestMapping("/depts")
    @ResponseBody
    public List<Department> getDepts(){
        List<Department> departments = departmentService.selectAll();
        return departments;
    }



    @RequestMapping("/trans")
    public String trans() throws IOException {
        /*//老方法转换
        Department department = new Department(1l, "张三", "zs");
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(department); //对象转JSON字符串
        System.out.println(s);
        String s1="{\"id\":1,\"name\":\"张三\",\"sn\":\"zs\"}";
        System.out.println(objectMapper.readValue(s1, Department.class));   //JSON字符串转对象

        //阿里API快速转
        String s2 = JSON.toJSONString(department);//对象转JSON
        System.out.println("阿里"+s2);

        Department department1 = JSON.parseObject(s1, Department.class);
        System.out.println("阿里"+department);
        //创建*/
        return null;
    }
}
